package com.example.demo.shmanage.service.impl;

import com.example.demo.common.util.Lunar;
import com.example.demo.shmanage.mapper.SHMapper;
import com.example.demo.shmanage.service.SHService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
@Transactional
public class SHServiceImpl implements SHService {
    @Autowired
    private SHMapper shMapper;

    /**
     * @Description 通过品牌名称查询所有品牌系列
     * @Author zhangxue
     * @Date 17:34 2018/7/11
     * @Param
     * @retrun
     **/
    @Override
    public Map<String, Object> queryBrandListByBrand(Map<String, Object> param) {
        List<Map<String,Object>> resultList = new ArrayList<Map<String,Object>>();
        resultList = shMapper.queryBrandListByBrand(param);
        Map<String,Object> resultMap = new HashMap<String, Object>();
        resultMap.put("BrandListJsonList",resultList);
        return resultMap;

    }

    /**
         * @Description 查询所有品牌名称
         * @Author zhangxue
         * @Date 17:20 2018/9/7
         * @Param
         * @retrun
         **/
    @Override
    public Map<String, Object> queryBrandList(Map<String, Object> param) {
        List<Map<String,Object>> resultList = new ArrayList<Map<String,Object>>();
        resultList = shMapper.queryBrandList(param);
        Map<String,Object> resultMap = new HashMap<String, Object>();
        resultMap.put("BrandList",resultList);
        return resultMap;

    }

    /**
     * @Description 通过品牌名称查询所有系列
     * @Author zhangxue
     * @Date 17:20 2018/9/7
     * @Param
     * @retrun
     **/
    @Override
    public Map<String, Object> querySeriesByBrandName(Map<String, Object> param) {
        List<Map<String,Object>> resultList = new ArrayList<Map<String,Object>>();
        resultList = shMapper.querySeriesByBrandName(param);
        Map<String,Object> resultMap = new HashMap<String, Object>();
        resultMap.put("SeriesList",resultList);
        return resultMap;

    }


    /**
     * @Description 根据条件查询所有商户信息
     * @Author zhangxue
     * @Date 17:34 2018/7/11
     * @Param
     * @retrun
     **/
    @Override
    public Map<String, Object> queryAllSHInfos(Map<String, Object> param) {
        String Yang = param.get("YangBirth")!=null||!"".equals(param.get("YangBirth"))?param.get("YangBirth").toString():null;
        if(Yang!=null){
            String[] QueryYear = Yang.split("/");
            String queryyear = QueryYear[0];
            param.put("queryyear",queryyear);
        }
        String yangstart = "";
        String yangend = "";
        String yinstart = "";
        String yinend = "";
        if(Yang!=null||!"".equals(Yang)){
            try {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
                Calendar c = Calendar.getInstance();
                c.setTime(sdf.parse(Yang));
                c.set(Calendar.DAY_OF_MONTH, 1);
                yangstart =sdf.format(c.getTime());
                c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
                yangend=sdf.format(c.getTime());

                //阳历日期转阴历日期
                Date tempyangstart = new SimpleDateFormat("yyyy/MM/dd").parse(yangstart);
                yangstart = new SimpleDateFormat("yyyy-MM-dd").format(tempyangstart);
                yinstart = Lunar.solarTolunar(yangstart);
                Date tempyangend = new SimpleDateFormat("yyyy/MM/dd").parse(yangend);
                yangend = new SimpleDateFormat("yyyy-MM-dd").format(tempyangend);
                yinend = Lunar.solarTolunar(yangend);
                if(yinstart!=null){
                    String[] YinQueryYearStart = yinstart.split("-");
                    String yinqueryyearstart = YinQueryYearStart[0];
                    param.put("yinqueryyearstart",yinqueryyearstart);
                }
                if(yangend!=null){
                    String[] YinQueryYearEnd = yangend.split("-");
                    String yinqueryyearend = YinQueryYearEnd[0];
                    param.put("yinqueryyearend",yinqueryyearend);
                }

            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        param.put("YangStart",yangstart);
        param.put("YangEnd",yangend);
        param.put("YinStart",yinstart);
        param.put("YinEnd",yinend);
        List<Map<String,Object>> resultList = new ArrayList<Map<String,Object>>();
        String BrandName = param.get("BrandJson")!=null&&!"".equals(param.get("BrandJson"))?param.get("BrandJson").toString():null;
        if(BrandName == null){
            resultList = shMapper.queryAllSHInfosMapperNull(param);
            Map<String,String> countMap = shMapper.queryAllSHInfosCountMapperNull(param);
            Object totalSize = countMap.get("total");
            Map<String,Object> resultMap = new HashMap<String, Object>();
            List<Map<String,Object>> shList = new ArrayList<Map<String,Object>>();
            shList = shMapper.queryAllSHCountMapperNull(param);
            int shTotal = 0;
            if(shList!=null){
                shTotal = shList.size();
            }
            resultMap.put("data",resultList);
            resultMap.put("shTotal",shTotal);
            resultMap.put("totalnum",totalSize);
            return resultMap;
        }else{
            resultList = shMapper.queryAllSHInfosMapper(param);
            Map<String,String> countMap = shMapper.queryAllSHInfosCountMapper(param);
            Object totalSize = countMap.get("total");
            Map<String,Object> resultMap = new HashMap<String, Object>();
            List<Map<String,Object>> shList = new ArrayList<Map<String,Object>>();
            shList = shMapper.queryAllSHCountMapper(param);
            int shTotal = 0;
            if(shList!=null){
                shTotal = shList.size();
            }
            resultMap.put("data",resultList);
            resultMap.put("shTotal",shTotal);
            resultMap.put("totalnum",totalSize);
            return resultMap;
        }
    }

    /**
         * @Description 新增商户管理信息
         * @Author zhangxue
         * @Date 11:18 2018/7/12
         * @Param
         * @retrun
         **/
    @Override
    public Map<String, Object> saveSHInfo(Map<String, Object> param) {
        Map<String,Object> resultMap = new HashMap<String, Object>();
        List<Map<String,Object>> ShList = new ArrayList<Map<String, Object>>();
        ShList = shMapper.queryShIsExistByCardNo(param);
        if(ShList.size()>0){
            resultMap.put("status","-1");
            return resultMap;
        }
        int i = shMapper.saveSHInfoMapper(param);
        if(i==1){
            resultMap.put("status","1");
        }else{
            resultMap.put("status","0");
        }
        return resultMap;
    }

    /**
     * @Description 删除商户信息
     * @Author zhangxue
     * @Date 14:31 2018/7/13
     * @Param
     * @retrun
     **/
    @Override
    public Map<String, Object> delSHInfo(Map<String, Object> param) {
        int i = shMapper.delSHInfoMapper(param);
        int j = shMapper.delSHZwInfoMapper(param);
        int k = shMapper.delSHImageInfoMapper(param);
        Map<String,Object> resultMap = new HashMap<String, Object>();
        if(i==1){
            resultMap.put("status","1");
        }else{
            resultMap.put("status","0");
        }
        return resultMap;
    }

    /**
         * @Description 修改商户信息
         * @Author zhangxue
         * @Date 17:48 2018/7/13
         * @Param
         * @retrun
         **/
    @Override
    public Map<String, Object> editSHInfo(Map<String, Object> param) {
        int i = shMapper.editSHInfoMapper(param);
        Map<String,Object> resultMap = new HashMap<String, Object>();
        if(i==1){
            resultMap.put("status","1");
        }else{
            resultMap.put("status","0");
        }
        return resultMap;
    }

    /**
         * @Description 添加授权书资料路径
         * @Author zhangxue
         * @Date 11:38 2018/7/13
         * @Param
         * @retrun
         **/
    @Override
    public Map<String, Object> saveUploadBook(Map<String, Object> param) {
        int i = shMapper.saveUploadBookMapper(param);
        Map<String,Object> resultMap = new HashMap<String, Object>();
        if(i==1){
            resultMap.put("status","1");
        }else{
            resultMap.put("status","0");
        }
        return resultMap;
    }

    /**
         * @Description 添加执照路径
         * @Author zhangxue
         * @Date 16:39 2018/7/16
         * @Param
         * @retrun
         **/
    @Override
    public Map<String, Object> saveUploadLicense(Map<String, Object> param) {
        int i = shMapper.saveUploadLicenseMapper(param);
        Map<String,Object> resultMap = new HashMap<String, Object>();
        if(i==1){
            resultMap.put("status","1");
        }else{
            resultMap.put("status","0");
        }
        return resultMap;
    }

    /**
     * @Description 添加身份证照片
     * @Author zhangxue
     * @Date 16:39 2018/7/16
     * @Param
     * @retrun
     **/
    @Override
    public Map<String, Object> saveUploadCard(Map<String, Object> param) {
        int i = shMapper.saveUploadCardMapper(param);
        Map<String,Object> resultMap = new HashMap<String, Object>();
        if(i==1){
            resultMap.put("status","1");
        }else{
            resultMap.put("status","0");
        }
        return resultMap;
    }

    /**
     * @Description 添加质检报告照片
     * @Author zhangxue
     * @Date 16:39 2018/7/16
     * @Param
     * @retrun
     **/
    @Override
    public Map<String, Object> saveUploadZjReport(Map<String, Object> param) {
        int i = shMapper.saveUploadCardMapper(param);
        Map<String,Object> resultMap = new HashMap<String, Object>();
        if(i==1){
            resultMap.put("status","1");
        }else{
            resultMap.put("status","0");
        }
        return resultMap;
    }

    /**
     * @Description 添加管理图纸及相关说明照片
     * @Author zhangxue
     * @Date 16:39 2018/7/16
     * @Param
     * @retrun
     **/
    @Override
    public Map<String, Object> saveUploadManageImg(Map<String, Object> param) {
        int i = shMapper.saveUploadCardMapper(param);
        Map<String,Object> resultMap = new HashMap<String, Object>();
        if(i==1){
            resultMap.put("status","1");
        }else{
            resultMap.put("status","0");
        }
        return resultMap;
    }

    /**
     * @Description 添加商标注册照片
     * @Author zhangxue
     * @Date 16:39 2018/7/16
     * @Param
     * @retrun
     **/
    @Override
    public Map<String, Object> saveUploadLogo(Map<String, Object> param) {
        int i = shMapper.saveUploadLogo(param);
        Map<String,Object> resultMap = new HashMap<String, Object>();
        if(i==1){
            resultMap.put("status","1");
        }else{
            resultMap.put("status","0");
        }
        return resultMap;
    }

    /**
     * @Description 添加生产许可证
     * @Author zhangxue
     * @Date 16:39 2018/7/16
     * @Param
     * @retrun
     **/
    @Override
    public Map<String, Object> saveProductPermise(Map<String, Object> param) {
        int i = shMapper.saveProductPermise(param);
        Map<String,Object> resultMap = new HashMap<String, Object>();
        if(i==1){
            resultMap.put("status","1");
        }else{
            resultMap.put("status","0");
        }
        return resultMap;
    }

    /**
     * @Description 添加产品使用说明书
     * @Author zhangxue
     * @Date 16:39 2018/7/16
     * @Param
     * @retrun
     **/
    @Override
    public Map<String, Object> saveUseExplain(Map<String, Object> param) {
        int i = shMapper.saveUseExplain(param);
        Map<String,Object> resultMap = new HashMap<String, Object>();
        if(i==1){
            resultMap.put("status","1");
        }else{
            resultMap.put("status","0");
        }
        return resultMap;
    }

    /**
     * @Description 添加产品图册
     * @Author zhangxue
     * @Date 16:39 2018/7/16
     * @Param
     * @retrun
     **/
    @Override
    public Map<String, Object> saveProductPhoto(Map<String, Object> param) {
        int i = shMapper.saveProductPhoto(param);
        Map<String,Object> resultMap = new HashMap<String, Object>();
        if(i==1){
            resultMap.put("status","1");
        }else{
            resultMap.put("status","0");
        }
        return resultMap;
    }

    /**
     * @Description 添加相关荣誉证书
     * @Author zhangxue
     * @Date 16:39 2018/7/16
     * @Param
     * @retrun
     **/
    @Override
    public Map<String, Object> saveHonorBook(Map<String, Object> param) {
        int i = shMapper.saveHonorBook(param);
        Map<String,Object> resultMap = new HashMap<String, Object>();
        if(i==1){
            resultMap.put("status","1");
        }else{
            resultMap.put("status","0");
        }
        return resultMap;
    }

    @Override
    public Map<String, Object> saveOtherInfo(Map<String, Object> param) {
        int i = shMapper.saveOtherInfo(param);
        Map<String,Object> resultMap = new HashMap<String, Object>();
        if(i==1){
            resultMap.put("status","1");
        }else{
            resultMap.put("status","0");
        }
        return resultMap;
    }

    /**
     * @Description 删除不同类型图片
     * @Author zhangxue
     * @Date 17:00 2018/7/13
     * @Param
     * @retrun
     **/
    @Override
    public Map<String, Object> delSHImage(Map<String, Object> param) {
        int i = shMapper.delSHImage(param);
        Map<String,Object> resultMap = new HashMap<String, Object>();
        if(i==1){
            resultMap.put("status","1");
        }else{
            resultMap.put("status","0");
        }
        return resultMap;
    }

    /**
     * @Description 查看商户详情
     * @Author zhangxue
     * @Date 16:17 2018/7/13
     * @Param
     * @retrun
     **/
    @Override
    public Map<String, Object> initLookInfo(Map<String, Object> param) {
        Map<String,Object> tempMap = shMapper.querySHInfoByID(param);
        return tempMap;
    }

    /**
         * @Description 查询不同类型上传图片
         * @Author zhangxue
         * @Date 9:14 2018/9/12
         * @Param
         * @retrun
         **/
    @Override
    public Map<String, Object> queryImgList(Map<String, Object> param) {
        List<Map<String,Object>> resultList1 = new ArrayList<Map<String,Object>>();
        resultList1 = shMapper.queryUploadBookList(param);
        Map<String,Object> resultMap = new HashMap<String, Object>();
        resultMap.put("data1",resultList1);

        List<Map<String,Object>> resultList2 = new ArrayList<Map<String,Object>>();
        resultList2 = shMapper.queryLicenseList(param);
        resultMap.put("data2",resultList2);

        List<Map<String,Object>> resultList3 = new ArrayList<Map<String,Object>>();
        resultList3 = shMapper.queryCardList(param);
        resultMap.put("data3",resultList3);

        List<Map<String,Object>> resultList4 = new ArrayList<Map<String,Object>>();
        resultList4 = shMapper.queryZjReportList(param);
        resultMap.put("data4",resultList4);

        List<Map<String,Object>> resultList5 = new ArrayList<Map<String,Object>>();
        resultList5 = shMapper.queryManageImgList(param);
        resultMap.put("data5",resultList5);
        //商标注册
        List<Map<String,Object>> resultList6 = new ArrayList<Map<String,Object>>();
        resultList6 = shMapper.queryLogoList(param);
        resultMap.put("data6",resultList6);
        //生产许可证
        List<Map<String,Object>> resultList7 = new ArrayList<Map<String,Object>>();
        resultList7 = shMapper.queryProductPremiseList(param);
        resultMap.put("data7",resultList7);

        //产品使用说明书
        List<Map<String,Object>> resultList8 = new ArrayList<Map<String,Object>>();
        resultList8 = shMapper.queryUseExplainList(param);
        resultMap.put("data8",resultList8);

        //产品图册
        List<Map<String,Object>> resultList9 = new ArrayList<Map<String,Object>>();
        resultList9 = shMapper.queryProductPhotoList(param);
        resultMap.put("data9",resultList9);

        //荣誉证书
        List<Map<String,Object>> resultList10 = new ArrayList<Map<String,Object>>();
        resultList10 = shMapper.queryHonorBookList(param);
        resultMap.put("data10",resultList10);

        //查询其他注意事项
        Map<String,Object> OtherInfoMap = new HashMap<String, Object>();
        OtherInfoMap = shMapper.queryOtherInfo(param);
        if(OtherInfoMap==null){
            resultMap.put("OtherInfo","");
        }else{
            String OtherInfo = OtherInfoMap.get("OtherInfo")!=null?OtherInfoMap.get("OtherInfo").toString():null;
            resultMap.put("OtherInfo",OtherInfo);
        }
        return resultMap;

    }

    /**
     * @Description 初始化展位编辑界面
     * @Author zhangxue
     * @Date 16:17 2018/7/13
     * @Param
     * @retrun
     **/
    @Override
    public Map<String, Object> initZWEditPage(Map<String, Object> param) {
        Map<String,Object> tempMap = shMapper.queryZwInfosByID(param);
        return tempMap;
    }

    /**
     * @Description 根据条件查询所有展位信息
     * @Author zhangxue
     * @Date 17:34 2018/7/11
     * @Param
     * @retrun
     **/
    @Override
    public Map<String, Object> queryAllZWInfos(Map<String, Object> param) {
        List<Map<String,Object>> resultList = new ArrayList<Map<String,Object>>();
        resultList = shMapper.queryAllZWInfosMapper(param);
        Map<String,String> countMap = shMapper.queryAllZWInfosCountMapper(param);
        Object totalSize = countMap.get("total");
        Map<String,Object> resultMap = new HashMap<String, Object>();
        Double StandAreaTotal = 0.00;
        Double EndStandAreaTotal = 0.00;
        Double StartStandAreaTotal = 0.00;
        Double StartDMStandAreaTotal = 0.00;
        Double StartOtherStandAreaTotal = 0.00;
        for(int i=0;i<resultList.size();i++){
            if("已撤场".equals(resultList.get(i).get("Type").toString())){
                EndStandAreaTotal += Double.parseDouble(resultList.get(i).get("StandArea").toString());
            }
            if("正常".equals(resultList.get(i).get("Type").toString())){
                StartStandAreaTotal += Double.parseDouble(resultList.get(i).get("StandArea").toString());
                if("东明店面".equals(resultList.get(i).get("StoreType").toString())){
                    StartDMStandAreaTotal += Double.parseDouble(resultList.get(i).get("StandArea").toString());
                }else{
                    StartOtherStandAreaTotal += Double.parseDouble(resultList.get(i).get("StandArea").toString());
                }
            }
            List<Map<String,Object>> BrandList = new ArrayList<Map<String, Object>>();
            StandAreaTotal +=Double.parseDouble(resultList.get(i).get("StandArea").toString());
            String BID = resultList.get(i).get("BID")!=null&&!"".equals(resultList.get(i).get("BID").toString())?resultList.get(i).get("BID").toString():null;
            if(BID!=null){
                //通过“,”拆分BID
                String[] BrandId = BID.split(",");
                for(int j=0;j<BrandId.length;j++){
                    Map<String,Object> tempBIDMap = new HashMap<String, Object>();
                    tempBIDMap.put("BrandID",BrandId[j]);
                    Map<String,Object> tempMap = new HashMap<String, Object>();
                    tempMap = shMapper.queryBrandInfosByID(tempBIDMap);
                    BrandList.add(tempMap);
                }
                //得到品牌名称
                resultList.get(i).put("BrandName",BrandList.get(0).get("BrandName").toString());
                //拼接系列名称
                String SeriesName = "";
                for(int k=0;k<BrandList.size();k++){
                    if(k==0){
                        SeriesName = BrandList.get(k).get("SeriesName").toString();
                    }else{
                        SeriesName += "," + BrandList.get(k).get("SeriesName").toString();
                    }

                }
                //得到拼接系列名称
                resultList.get(i).put("SeriesName",SeriesName);
            }
        }

        resultMap.put("data",resultList);
        resultMap.put("totalnum",totalSize);
        String StandAreaResult = String.format("%.2f", StandAreaTotal);
        String EndStandAreaTotalResult = String.format("%.2f", EndStandAreaTotal);
        String StartStandAreaTotalResult = String.format("%.2f", StartStandAreaTotal);
        String StartDMStandAreaTotalResult = String.format("%.2f", StartDMStandAreaTotal);
        String StartOtherStandAreaTotalResult = String.format("%.2f", StartOtherStandAreaTotal);
        resultMap.put("StandAreaTotal",StandAreaResult);
        resultMap.put("EndStandAreaTotal",EndStandAreaTotalResult);
        resultMap.put("StartStandAreaTotal",StartStandAreaTotalResult);
        resultMap.put("StartDMStandAreaTotal",StartDMStandAreaTotalResult);
        resultMap.put("StartOtherStandAreaTotal",StartOtherStandAreaTotalResult);
        return resultMap;

    }

    /**
     * @Description 新增展位信息
     * @Author zhangxue
     * @Date 15:10 2018/7/13
     * @Param
     * @retrun
     **/
    @Override
    public Map<String, Object> saveZWInfo(Map<String, Object> param) {
        List<String> seriesList = (List<String>) param.get("SeriesName");
        String seriesStr = "";
        for(int i=0;i<seriesList.size();i++){
            if(i==0){
                seriesStr = String.valueOf(seriesList.get(0));
            }
            if(i>0){
                seriesStr += ","+String.valueOf(seriesList.get(i));
            }
        }
        param.put("seriesStr",seriesStr);
        int i = shMapper.saveZWInfoMapper(param);
        Map<String,Object> resultMap = new HashMap<String, Object>();
        if(i==1){
            resultMap.put("status","1");
        }else{
            resultMap.put("status","0");
        }
        return resultMap;
    }

    /**
         * @Description 删除展位信息
         * @Author zhangxue
         * @Date 17:00 2018/7/13
         * @Param
         * @retrun
         **/
    @Override
    public Map<String, Object> delZWInfo(Map<String, Object> param) {
        int i = shMapper.delZWInfoMapper(param);
        Map<String,Object> resultMap = new HashMap<String, Object>();
        if(i==1){
            resultMap.put("status","1");
        }else{
            resultMap.put("status","0");
        }
        return resultMap;
    }

    /**
     * @Description 编辑展位撤场信息
     * @Author zhangxue
     * @Date 17:00 2018/7/13
     * @Param
     * @retrun
     **/
    @Override
    public Map<String, Object> editZWInfo(Map<String, Object> param) {
        List<String> seriesList = (List<String>) param.get("SeriesName");
        String seriesStr = "";
        for(int i=0;i<seriesList.size();i++){
            if(i==0){
                seriesStr = String.valueOf(seriesList.get(0));
            }
            if(i>0){
                seriesStr += ","+String.valueOf(seriesList.get(i));
            }
        }
        param.put("seriesStr",seriesStr);
        String EndTime = param.get("EndTime")!=null&&!"".equals(param.get("EndTime"))?param.get("EndTime").toString():null;
        param.put("EndTime",EndTime);
        int i = shMapper.editZWInfoMapper(param);
        Map<String,Object> resultMap = new HashMap<String, Object>();
        if(i==1){
            resultMap.put("status","1");
        }else{
            resultMap.put("status","0");
        }
        return resultMap;
    }

    /**
     * @Description 新增品牌与品牌系列
     * @Author zhangxue
     * @Date 11:18 2018/7/12
     * @Param
     * @retrun
     **/
    @Override
    public Map<String, Object> saveBrandAndSeries(Map<String, Object> param) {
        List<Map<String,Object>> existBrandList = new ArrayList<Map<String, Object>>();
        existBrandList = shMapper.queryExistBrand(param);
        Map<String,Object> resultMap = new HashMap<String, Object>();
        if(existBrandList.size()!=0){
            resultMap.put("status","2");//代表已有该系列，无法保存
            return resultMap;
        }
        int i = shMapper.saveBrandAndSeries(param);
        if(i==1){
            resultMap.put("status","1");
        }else{
            resultMap.put("status","0");
        }
        return resultMap;
    }

}
